package com.beautiful.xin.list;

import com.sunday.springdebug.leecode.LeecodeTest;

/**
 * @author xuqingxin 2021/7/8 11:24 上午
 */
public class MergeList {

    public static void main(String[] args) {
        int[] nums1 = {1, 3, 5, 6, 7, 8};
        int[] nums2 = {4, 7, 9};

        int[] ints = MergeList.mergeList(nums1, nums2);
        for (int anInt : ints) {
            System.out.print(anInt);
        }
    }

    public static int[] mergeList(int[] arr1, int[] arr2) {
        int[] result = new int[arr1.length + arr2.length];
        int i = 0, j = 0, k = 0;
        while (i < arr1.length && j < arr2.length) {
            if (arr1[i] <= arr2[j]) {
                result[k++] = arr1[i++];
            } else {
                result[k++] = arr2[j++];
            }
        }

        while (i < arr1.length) {
            result[k++] = arr1[i++];
        }
        while (j < arr2.length) {
            result[k++] = arr2[j++];
        }
        return result;
    }

    public static int[] merge2(int[] arr1, int[] arr2) {
        int[] result = new int[arr1.length + arr2.length];
        int i = 0, j = 0, k = 0;
        while (i < arr1.length && j < arr2.length) {
            if (arr1[i] < arr2[j]) {
                result[k++] = arr1[i++];
            } else {
                result[k++] = arr2[j++];
            }
        }

        while (i < arr1.length) {
            result[k++] = arr1[i++];
        }
        while (j < arr2.length) {
            result[k++] = arr2[j++];
        }
        return result;
    }


}
